package org.spring.springboot.app.adv.advscreeninfo.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.spring.springboot.app.adv.advscreeninfo.domain.po.AdvScreenInfoPO;
import org.spring.springboot.app.adv.advscreeninfo.domain.vo.*;
import org.spring.springboot.app.adv.advscreeninfo.service.IAdvScreenInfoService;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.base.Type;
import org.spring.springboot.app.base.User;
import org.spring.springboot.component.annotation.AccessTokenVerify;
import org.spring.springboot.util.ApiIndex;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Api(description="广告屏广告操作接口",tags = ApiIndex.AdvScreenInfoController)
@RequestMapping(value = "/api/advScreenInfo")
@RestController
@AccessTokenVerify
public class AdvScreenInfoController {

    @Autowired
    private IAdvScreenInfoService advScreenInfoService;

    @ApiOperation(value = "查询分页列表")
    @RequestMapping(value = "", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R<PageInfo<List<AdvScreenInfoQueryListVO>>> findPageForMyPublic(
            @ApiIgnore User user,
            @ApiParam(value = "页数", required = true) @RequestParam(defaultValue = "1") int pageNum,
            @ApiParam(value = "每页条数", required = true) @RequestParam(defaultValue = "10") int pageSize,
            @ApiParam(value = "查询类型 1我发布的 2我申请的 3我接受的 4系统", required = true) @RequestParam(defaultValue = "1") int queryType,
            @ApiParam(value = "标题", required = false) @RequestParam(defaultValue = "") String title,
            @ApiParam(value = "代理商ID", required = false) @RequestParam(defaultValue = "") String officeId
    ) {
        R result = new R();
        AdvScreenInfoSearchVO searchVO = new AdvScreenInfoSearchVO();
        if(StringUtils.isBlank(officeId)){
            officeId=user.getOfficeId();
        }
        searchVO.setOfficeId(officeId);
        searchVO.setQueryType(queryType);
        searchVO.setTitle(title);
        List<AdvScreenInfoPO> list = advScreenInfoService.findPageForMyPublic(searchVO, pageNum, pageSize);
        PageInfo p = new PageInfo(list);
        p.setList(getVOS(p.getList()));
        result.setData(p);
        return result;
    }

    private List<AdvScreenInfoQueryListVO> getVOS(List<AdvScreenInfoPO> _list) {
        List<AdvScreenInfoQueryListVO> list = new ArrayList<AdvScreenInfoQueryListVO>();
        if (_list != null && _list.size()>0) {
            for (AdvScreenInfoPO po : _list) {
                AdvScreenInfoQueryListVO vo = new AdvScreenInfoQueryListVO();
                BeanUtils.copyProperties(po,vo);
                list.add(vo);
            }
        }
        return list;
    }

    @ApiOperation(value = "查询单个信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R<AdvScreenInfoQueryDetailVO> find(
            @ApiParam(value = "id", required = true) @PathVariable("id") String id
    ) {
        R result = new R();
        AdvScreenInfoPO po = advScreenInfoService.find(id);
        AdvScreenInfoQueryDetailVO vo = new AdvScreenInfoQueryDetailVO();
        BeanUtils.copyProperties(po,vo);
        result.setData(vo);
        return result;
    }


    @ApiOperation(value="删除信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R delete(@ApiIgnore User user,@ApiParam(value = "id", required = true)  @PathVariable("id") String id) {
        R result = new R();
        if (StringUtils.isBlank(id)) {
            result.setType(Type.ParamValidateFail);
            result.setMessage("id不能为空");
            return result;
        }
        return advScreenInfoService.delete(id);
    }




    @ApiOperation(value = "支付广告")
    @RequestMapping(value = "/pay", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R pay(
            @ApiIgnore User user,
            @ApiParam(value = "广告id", required = false) @RequestParam(defaultValue = "") String id
    ) {
        R result = new R();
        if (StringUtils.isBlank(id)) {
            result.setType(Type.ParamValidateFail);
            result.setMessage("id不能为空");
            return result;
        }
        return advScreenInfoService.pay(id,user);
    }




    @ApiOperation(value="添加信息-我发布")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @RequestMapping(value = "/my_public", method = RequestMethod.POST)
    public R<AdvScreenInfoForMyPublicSaveOutVO> saveForMyPublic(@ApiIgnore User user, @ApiParam(value = "对象", required = true) @RequestBody AdvScreenInfoForMyPublicSaveInVO vo, BindingResult bindingResult) {
        R result = new R();
        if (bindingResult.hasErrors()) {
            result.setCode(1);
            result.setMessage(bindingResult.getAllErrors().get(0).toString());
            result.setMessages(bindingResult.getAllErrors());
            return result;
        }
        return advScreenInfoService.saveForMyPublic(vo,user);
    }

    @ApiOperation(value="修改信息",notes = "只能修改未审核的广告")
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R update(@ApiIgnore User user, @ApiParam(value = "对象", required = true) @Valid  @RequestBody AdvScreenInfoUpdateInVO vo, BindingResult bindingResult) {
        R result = new R();
        if (bindingResult.hasErrors()) {
            result.setCode(1);
            result.setMessage(bindingResult.getAllErrors().get(0).toString());
            result.setMessages(bindingResult.getAllErrors());
            return result;
        }
        return advScreenInfoService.updateInfo(vo);
    }


    @ApiOperation(value="添加信息-我申请")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @RequestMapping(value = "/my_askfor", method = RequestMethod.POST)
    public R<AdvScreenInfoForMyPublicSaveOutVO> saveForMyAskFor(@ApiIgnore User user, @ApiParam(value = "对象", required = true) @RequestBody AdvScreenInfoForMyAskForSaveInVO vo, BindingResult bindingResult) {
        R result = new R();
        if (bindingResult.hasErrors()) {
            result.setCode(1);
            result.setMessage(bindingResult.getAllErrors().get(0).toString());
            result.setMessages(bindingResult.getAllErrors());
            return result;
        }
        return advScreenInfoService.saveForMyAskFor(vo,user);
    }

    @ApiOperation(value="审核信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @RequestMapping(value = "/my_exam", method = RequestMethod.POST)
    public R updateForMyExam(@ApiIgnore User user, @ApiParam(value = "对象", required = true) @RequestBody AdvScreenInfoForMyExamUpdateInVO vo, BindingResult bindingResult) {
        R result = new R();
        if (bindingResult.hasErrors()) {
            result.setCode(1);
            result.setMessage(bindingResult.getAllErrors().get(0).toString());
            result.setMessages(bindingResult.getAllErrors());
            return result;
        }
        return advScreenInfoService.updateForMyExam(vo,user);
    }

    @ApiOperation(value="添加信息-系统")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @RequestMapping(value = "/system_public", method = RequestMethod.POST)
    public R saveForSystem(@ApiIgnore User user, @ApiParam(value = "对象", required = true) @RequestBody AdvScreenInfoForSystemSaveInVO vo, BindingResult bindingResult) {
        R result = new R();
        if (bindingResult.hasErrors()) {
            result.setCode(1);
            result.setMessage(bindingResult.getAllErrors().get(0).toString());
            result.setMessages(bindingResult.getAllErrors());
            return result;
        }
        return advScreenInfoService.saveForSystem(vo,user);
    }




    @ApiOperation(value = "消耗详情（广告日志）")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @RequestMapping(value = "/findPageLog", method = RequestMethod.GET)
    public R<PageInfo<List<AdvScreenLogVO>>> findPageLog(
            @ApiParam(value = "所属广告ID", required = true) @RequestParam(defaultValue = "") String advId,
            @ApiParam(value = "所属设备ID", required = false) @RequestParam(defaultValue = "") String hardId,
            @ApiParam(value = "观看人名称", required = false) @RequestParam(defaultValue = "") String userName,
            @ApiParam(value = "开始观看时间（例如1993-08-11 或者 1993-08-11 00:00:00）", required = false) @RequestParam(defaultValue = "") String startCreateDate,
            @ApiParam(value = "结束观看时间（例如1993-08-11 或者 1993-08-11 23:59:59）", required = false) @RequestParam(defaultValue = "") String endCreateDate,
            @ApiParam(value = "页数", required = true) @RequestParam(defaultValue = "1") int pageNum,
            @ApiParam(value = "每页条数", required = true) @RequestParam(defaultValue = "10") int pageSize
    ) {
        R result = new R();
        AdvScreenLogSearchVO searchVO = new AdvScreenLogSearchVO();
        searchVO.setAdvId(advId);
        searchVO.setHardId(hardId);
        searchVO.setUserName(userName);
        if(StringUtils.isNotBlank(startCreateDate)){
            if(startCreateDate.indexOf(":")==-1){
                startCreateDate=startCreateDate+" 00:00:00";
            }
        }
        if(StringUtils.isNotBlank(endCreateDate)){
            if(endCreateDate.indexOf(":")==-1){
                endCreateDate=endCreateDate+" 23:59:59";
            }
        }
        searchVO.setStartCreateDate(startCreateDate);
        searchVO.setEndCreateDate(endCreateDate);
        List<AdvScreenLogVO> list = advScreenInfoService.findPageForMyPublicLog(searchVO, pageNum, pageSize);
        PageInfo p = new PageInfo(list);
        p.setList(list);
        result.setData(p);
        return result;
    }

}
